package com.xbongbong.saas.service.toolbox.handle;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.saas.constant.TabConstant;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: wujian
 * @create: 2021-01-15 17:23
 * @description: 数组类型数据数据匹配规则抽象类
 **/
@Component
public class WebListDataValueMatchHandle {
    public boolean match(ConditionsEntityExt conditionsEntityExt, JSONObject data){
        ConditionEnum conditionEnum = ConditionEnum.getConditionEnum(conditionsEntityExt.getSymbol());
        if (Objects.isNull(conditionEnum)) {
            return false;
        }
        String attr = conditionsEntityExt.getAttr();
        List<Object> value = conditionsEntityExt.getValue();
        JSONArray jsonArray = data.getJSONArray(attr);
        switch (conditionEnum) {
            case EQUAL:
            case INCLUDE:
            case IN:
            case LIKE:
                if (!emptyCheck(value, jsonArray, 3) ||  !jsonArray.contains(value.get(0))) {
                    if (Objects.equals(TabConstant.OWNERID, attr)) {
                        List<Object> valueList = new ArrayList<>(value);
                        valueList.retainAll(jsonArray);
                        return CollectionsUtil.isNotEmpty(valueList);
                    }
                    return false;
                }
                break;
            case NOEQUAL:
            case NOIN:
            case NOLIKE:
                if (!emptyCheck(value, jsonArray, 3) || jsonArray.contains(value.get(0))) {
                    return false;
                }
                break;
            case ALLINCLUDE:
                if (!emptyCheck(value, jsonArray, 3) || !Objects.equals(jsonArray.size(), value.size()) || !jsonArray.containsAll(value)) {
                    return false;
                }
                break;
            case EMPTY:
                if (Objects.nonNull(jsonArray) && !jsonArray.isEmpty()) {
                    return false;
                }
                break;
            case NOEMPTY:
                if (Objects.isNull(jsonArray) || jsonArray.isEmpty()) {
                    return false;
                }
                break;
            default:
                break;
        }
        return true;
    }

    private boolean emptyCheck(List<Object> value, JSONArray jsonArray, Integer checkType) {
        if (checkType == 1) {
            if (Objects.isNull(jsonArray) || jsonArray.isEmpty()) {
                return false;
            }
        } else if (checkType == 2) {
            if (Objects.isNull(value) || value.isEmpty()) {
                return false;
            }
        } else {
            if (Objects.isNull(jsonArray) || jsonArray.isEmpty() || Objects.isNull(value) || value.isEmpty()) {
                return false;
            }
        }
        return true;
    }
}
